<?php

class PembayaranController extends Controller
{
	
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionShowCicilan()
	{
		//if asuransi health
		$sql = " SELECT * FROM ndk_health order by due_date ";
		//if asuransi non health
		//$sql = " SELECT * FROM ndk_health_non order by due_date  ";
		
		$command = Yii::app()->db->createCommand($sql);
		$datas = $command->queryAll();
		$jsonData='{"jsonShowCicilan":[';
			foreach($datas as $data):
				$jsonData .='{"nominal":"'.$data['nominal'].'","premium":"'.$data['premium'].'","policy_cost":"'.$data['policy_cost'].'","admin_broker":"'.$data['admin_broker'].'","discount":"'.$data['discount'].'","adjusment":"'.$data['adjusment'].'","brokerage":"'.$data['brokerage'].'","ppn":"'.$data['ppn'].'","pph":"'.$data['pph'].'","due_date":"'.date('d-m-Y',strtotime($data['due_date'])).'","is_bayar":"'.$data['is_bayar'].'","id":"'.$data['id'].'","jml_bayar":"'.$data['jml_bayar'].'"},';
			endforeach;
			if(count($datas)!=0){
				$jsonData=substr($jsonData,0,strlen($jsonData)-1);
			}
			
			$jsonData .=']';
			
			$jsonData .='}';
	
			echo $jsonData;
			Yii::app()->end();
	}
	
	public function actionBayarCicilan()
	{
		if($_POST['jml_bayar'] >= $_POST['nominal'] || $_POST['jml_bayar'] >= ($_POST['nominal'] - 5000)){
			NdkHealth::model()->updateByPk($_POST['id'], array("is_bayar"=> '1',"jml_bayar"=>$_POST['jml_bayar']));
		}else{
			NdkHealth::model()->updateByPk($_POST['id'], array("jml_bayar"=>$_POST['jml_bayar']));
		}
		echo "1";
		exit;
	}
	
}